package com.ymm.platform.core.facade;

import com.github.pagehelper.PageInfo;
import com.ymm.platform.api.constants.dto.LltorderDetailsDTO;
import com.ymm.platform.api.constants.dto.OrderShowDTO;
import com.ymm.platform.api.constants.dto.QueryDriverDTO;
import com.ymm.platform.api.constants.entity.DriverEntity;
import com.ymm.platform.api.facade.PlatformQueryDriverService;
import com.ymm.platform.api.utils.JudgePagination;
import com.ymm.platform.api.utils.Result;
import com.ymm.platform.dao.mapper.QueryDriverMapper;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageHelper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 司机查询服务
 * @Author: Xuecheng Li
 * @CreateTime: 2022-08-03 12:30
 * @Version: 1.0.0
 */
@Slf4j
@Component
@Service
public class PlatformQueryDriverServiceImpl implements PlatformQueryDriverService {

    @Resource
    private QueryDriverMapper queryDriverMapper;

    private JudgePagination judgePagination=new JudgePagination();





    /**
    * @Author: chencong
    * @Description: 分页查询所有司机
    * @DateTime: 17:25 2022/8/12
    * @Params: [pageNum]
    * @Return com.ymm.platform.api.utils.Result<java.util.List<com.ymm.platform.api.constants.entity.DriverEntity>>
    */
    @Override
    public Result<PageInfo> queryAllDriver(Integer pageNum, Integer pageSize) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<DriverEntity> driverEntityList = queryDriverMapper.queryAllDriver();
        PageInfo<DriverEntity> pageInfo=new PageInfo(driverEntityList);
        return Result.success(pageInfo);
    }


    @Override
    public Result<PageInfo> queryDriverOnCondition(Integer pageNum, Integer pageSize, QueryDriverDTO queryDriverDTO) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);

        /*if(queryDriverDTO.equals(null)){
            return queryAllDriver(pageNum,pageSize);
        }
        PageHelper.startPage(pageNum, pageSize);
        if(!queryDriverDTO.getDriverIdNum().equals(null)){
            List<DriverEntity> driverEntityList = queryDriverMapper.queryDriverOnDriverIdNum(queryDriverDTO.getDriverIdNum());
            PageInfo<DriverEntity> pageInfo=new PageInfo(driverEntityList);
            return Result.success(pageInfo);
        }
        if(!queryDriverDTO.getDriverPhoneNum().equals(null)){
            List<DriverEntity> driverEntityList = queryDriverMapper.queryDriverOnDriverPhone(queryDriverDTO.getDriverPhoneNum());
            PageInfo<DriverEntity> pageInfo=new PageInfo(driverEntityList);
            return Result.success(pageInfo);
        }
        List<DriverEntity> driverEntityList = queryDriverMapper.queryDriverOnName(queryDriverDTO.getDriverName());*/
        PageHelper.startPage(pageNum, pageSize);
        List<DriverEntity> driverEntityList = queryDriverMapper.queryDriverOnConditionDTO(queryDriverDTO);
        PageInfo<DriverEntity> pageInfo=new PageInfo(driverEntityList);
        return Result.success(pageInfo);
    }

    @Override
    public Result<PageInfo> queryDriverOnValid(Integer pageNum, Integer pageSize) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<DriverEntity> driverEntityList = queryDriverMapper.queryDriverOnValid();
        PageInfo<DriverEntity> pageInfo=new PageInfo(driverEntityList);
        return Result.success(pageInfo);
    }


    @Override
    public Result<Long> queryDriverCompleteOrder(Long driverId) {
        return Result.success(queryDriverMapper.queryDriverCompleteOrder(driverId));
    }

    @Override
    public Result<PageInfo> queryDriverCompleteOrderList(Integer pageNum, Integer pageSize, Long driverId) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryDriverMapper.queryDriverCompleteOrderList(driverId);
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);
    }



    @Override
    public Result<Long> queryDriverUncompleteOrder(Long driverId) {

        return  Result.success(queryDriverMapper.queryDriverUncompleteOrder(driverId));
    }

    @Override
    public Result<PageInfo> queryDriverUncompleteOrderList(Integer pageNum, Integer pageSize, Long driverId) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryDriverMapper.queryDriverUncompleteOrderList(driverId);
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);
    }


    @Override
    public Result<Integer> queryDriverTotalIncome(Long driverId) {
        List<LltorderDetailsDTO> lltorderDetailsDTOS =queryDriverMapper.queryDriverCompleteOrderList(driverId);
        Integer s=0;
        for(LltorderDetailsDTO lltorderDetailsDTO:lltorderDetailsDTOS){
            s=s+lltorderDetailsDTO.getOrderPrice();
        }
        return Result.success(new Double(s*0.9).intValue());
    }


    /**
     * @Author: chencong
     * @Description: 将LltorderDetailsDTO转化为OrderShowDTO
     * @DateTime: 21:10 2022/8/15
     * @Params: [lltorderDetailsDTO]
     * @Return com.ymm.platform.api.constants.dto.OrderShowDTO
     */
    private OrderShowDTO orderDetailsToShow(LltorderDetailsDTO lltorderDetailsDTO){
        OrderShowDTO orderShowDTO = new OrderShowDTO();
        orderShowDTO.setOrderId(lltorderDetailsDTO.getOrderId());

        //orderShowDTO.setTradeId(lltorderDetailsDTO.getTradeId());
        orderShowDTO.setTradeDriverState(lltorderDetailsDTO.getTradeEntity().getTradeDriverState());
        orderShowDTO.setOrderShipperState(lltorderDetailsDTO.getOrderShipperState());
        orderShowDTO.setShipperName(lltorderDetailsDTO.getShipperEntity().getNickname());
        orderShowDTO.setShipperPhone(lltorderDetailsDTO.getShipperEntity().getPhoneNumber());
        orderShowDTO.setDriverName(lltorderDetailsDTO.getDriverEntity().getDriverName());
        orderShowDTO.setDriverPhone(lltorderDetailsDTO.getDriverEntity().getDriverPhone());
        orderShowDTO.setDriverType(lltorderDetailsDTO.getDriverEntity().getDriverType());
        orderShowDTO.setDriverCarNum(lltorderDetailsDTO.getDriverEntity().getDriverCarNum());
        orderShowDTO.setOrderCreateDate(lltorderDetailsDTO.getOrderCreateDate());
        orderShowDTO.setTradeArriveTime(lltorderDetailsDTO.getTradeEntity().getTradeArriveTime());
        orderShowDTO.setOrderPrice(lltorderDetailsDTO.getOrderPrice());
        orderShowDTO.setCargoSrc(lltorderDetailsDTO.getCargoEntity().getCargoSrc());
        orderShowDTO.setCargoDes(lltorderDetailsDTO.getCargoEntity().getCargoDes());
        orderShowDTO.setCargoTip(lltorderDetailsDTO.getCargoEntity().getCargoComment());
        orderShowDTO.setDriverCarType(lltorderDetailsDTO.getDriverEntity().getDriverCarType());


        return orderShowDTO;
    }

    @Override
    public Result<PageInfo> queryDriverListByName(Integer pageNum, Integer pageSize,String driverName){
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<DriverEntity> driverEntityList = queryDriverMapper.queryDriverOnName(driverName);

        PageInfo<DriverEntity> pageInfo=new PageInfo(driverEntityList);
        return Result.success(pageInfo);
    }

    @Override
    public Result<PageInfo> queryDriverListByPhone(Integer pageNum, Integer pageSize,String driverPhone){
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<DriverEntity> driverEntityList = queryDriverMapper.queryDriverOnDriverPhone(driverPhone);

        PageInfo<DriverEntity> pageInfo=new PageInfo(driverEntityList);
        return Result.success(pageInfo);
    }
}
